package com.keyinong.caizhengting.view;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.keyinong.caizhengting.R;
import com.keyinong.caizhengting.tools.HttpTools;
import com.keyinong.caizhengting.tools.Utility;
import com.keyinong.caizhengting.adapter.HongbaoAdapter;
import com.keyinong.caizhengting.entity.HongbaoEntity;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class HongbaoActivity extends Activity {
    ArrayList<HongbaoEntity> hongbaolist;
    HttpTools httptools = null;
    private HongbaoAdapter hongbaoadapter;
    private ImageButton ib_back;
    private ListView lv_hongbao;
    private TextView tv_more;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_hongbao);
        lv_hongbao = (ListView) findViewById(R.id.lv_hongbao);
        tv_more = (TextView) findViewById(R.id.tv_more);
        Hongbao();
        ib_back = (ImageButton) findViewById(R.id.ib_back);
        ib_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void Hongbao() {
        hongbaolist = new ArrayList<HongbaoEntity>();
        SharedPreferences sp = getSharedPreferences("spReg",
                Context.MODE_PRIVATE);
        String token = sp.getString("data", "");
//		Log.i("shi", "token---" + token);
        if (httptools == null) {
            httptools = new HttpTools(HongbaoActivity.this);
        }
        httptools.myCoupon(token, new JsonHongbao());
    }

    public class JsonHongbao extends JsonHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, Header[] headers,
                              JSONArray response) {
            // TODO Auto-generated method stub
            super.onSuccess(statusCode, headers, response);
            try {
                Log.i("info", response.toString());
                for (int i = 0; i < response.length(); i++) {
                    JSONObject data = response.getJSONObject(i);
                    String id = data.getString("id");
                    String more = data.getString("more");
                    String off = data.getString("off");
                    String starttime = data.getString("starttime");
                    String endtime = data.getString("endtime");
                    String note = data.getString("note");

                    hongbaolist.add(new HongbaoEntity(id, off, more, starttime, endtime, note));
                }
                if (response.length() == 0) {
                    tv_more.setVisibility(View.VISIBLE);
                } else {
                    tv_more.setVisibility(View.GONE);
                }
                hongbaoadapter = new HongbaoAdapter(HongbaoActivity.this, hongbaolist);
                lv_hongbao.setAdapter(hongbaoadapter);
                lv_hongbao.setDivider(null);
                Utility.setListViewHeightBasedOnChildren(lv_hongbao);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }


}
